package com.basic.thread;

class Ticket{
    int number = 10;

    public synchronized void sale(){
        if(number > 0){
            try {
                //模拟业务
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName()+"卖第"+(number--)+"张票，还剩"+number+"张票");
        }
    }
}
public class ThreadDemo {

    public static void main(String[] args) {
        Ticket ticket = new Ticket();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    ticket.sale();
                }
            }
        },"AA").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    ticket.sale();
                }
            }
        },"BB").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    ticket.sale();
                }
            }
        },"CC").start();
    }
}
